package labox.innovation.gameserver.idfactory;

import java.util.Stack;

import labox.innovation.config.Config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * This class ...
 * 
 * @version $Revision: 1.3.2.1.2.7 $ $Date: 2005/04/11 10:06:12 $
 */
public class StackIDFactory extends IdFactory {

	private static Logger _log = LoggerFactory.getLogger(IdFactory.class.getName());

	private int _curOID;

	private Stack<Integer> _freeOIDStack = new Stack<Integer>();

	protected StackIDFactory() {
		super();
		_curOID = FIRST_OID;

		try {
			_curOID++;
			_log.info("IdFactory: Next usable Object ID is: " + _curOID);
			_initialized = true;
		} catch (Exception e1) {
			_log.error("ID Factory could not be initialized correctly:", Config.SERVER_ID, e1);
		}
	}

	public static IdFactory getInstance() {
		return _instance;
	}

	@Override
	public synchronized int getNextId() {
		int id;
		if (!_freeOIDStack.empty())
			id = _freeOIDStack.pop();
		else {
			id = _curOID;
			_curOID = _curOID + 1;
		}
		return id;
	}

	/**
	 * return a used Object ID back to the pool
	 * 
	 * @param object
	 *            ID
	 */
	@Override
	public synchronized void releaseId(int id) {
		_freeOIDStack.push(id);
	}

	@Override
	public int size() {
		return FREE_OBJECT_ID_SIZE - _curOID + FIRST_OID + _freeOIDStack.size();
	}

	/* (non-Javadoc)
	 * @see labox.innovation.gameserver.idfactory.IdFactory#getNextId(int)
	 */
	@Override
	public long getNextId(int type) {
		// TODO Auto-generated method stub
		return 0;
	}
}